#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct edge
{
    int u, v, w;
    bool operator<(const edge &a)
    {
        return w < a.w;
    }
} edges[N];
vector<edge> ans;
int n, m;
int p[N];
int ret, cnt;


int find(int u)
{
    if (p[u] != u)
    {
        p[u] = find(p[u]);
    }
    return p[u];
}
bool kruskal()
{
    sort(edges, edges + m);
    // 便利每一条边
    for (int i = 0; i < m; i++)
    {
        int u = edges[i].u;
        int v = edges[i].v;
        int w = edges[i].w;
        int pu = find(u);
        int pv = find(v);
        if(pu!=pv)
        {
            p[pu]=pv;
            ret+=w;
            cnt++;
            ans.push_back({u,v,w});
        }
    }
    if(cnt<n-1) return false;
    else return true;
}
int main()
{
    cin>>n>>m;
    for (int i = 0; i < m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[i] = {u, v, w};
    }
    for (int i = 1; i <= n; i++)
        p[i] = i;
    if(kruskal()) 
    {
        cout<<ret<<endl;
        for(auto&ch:ans) cout<<ch.u<<" "<<ch.v<<" "<<ch.w<<endl;
    }
    else cout<<"impossible"<<endl;
    return 0;
}